﻿using System;
using System.Windows;
#if WPF
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Shapes;
#else
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Shapes;
using Windows.UI.Xaml.Data;
#endif

namespace Eternal.DataVisualization
{
    public class LineSeries : Performance
    {
        protected internal override void ApplyBrush(Shape element, int index)
        {
            element.StrokeThickness = StrokeThickness;
            element.Stroke = SegmentBrush ?? Area.GetBrush(index);
        }

        protected internal override void OnDrawSegment()
        {
            base.OnDrawSegment();
#if WPF_UN
            if (RenderPerformance == PerformanceType.Normal)
#endif
            {
                SegmentInfo segmentInfo;
                SegmentInfos.Clear();
                SegmentPanel.Children.Clear();
                double dataCount = ActualXData.Count;
                {
                    for (var i = (int)Math.Floor(XAxis.Start); i < (int)Math.Ceiling(XAxis.End); i++)
                    {
                        var line = new Line();
                        segmentInfo = new SegmentInfo();
                        SegmentPanel.Children.Add(line);
                        ApplyBrush(line, i);
                        SegmentInfos.Add(segmentInfo);
                        line.Tag = segmentInfo;
                        line.X1 = ValueToCoordianteXAxis(i);
                        line.X2 = ValueToCoordianteXAxis(i + 1);
                        line.Y1 = ValueToCoordianteYAxis(ActualYData[i]);
                        line.Y2 = ValueToCoordianteYAxis(ActualYData[i + 1]);
                    }
                }
            }
#if WPF_UN
            else
            {
                var actualBrush = GetBrush() as SolidColorBrush;
                var color = actualBrush.Color;
                    writeableBitmap.Lock();
                    writeableBitmap.Clear();
                    for (var i = (int)Math.Floor(MinimumX); i < (int)Math.Ceiling(MaximumX); i++)
                    {
                        writeableBitmap.DrawLineBresenham((int)ValueToCoordianteXAxis(i), (int)ValueToCoordianteYAxis(ActualYData[i]), (int)ValueToCoordianteXAxis(i + 1), (int)ValueToCoordianteYAxis(ActualYData[i + 1]), color);
                    }
                    writeableBitmap.Unlock();
            }
#endif
        }
    }
}
